package com.nue.controller;



import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.nue.pojo.UmsUser;
import com.nue.service.IUmsUserService;
import com.nue.util.CommonResult;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wk
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/ums-user")
public class UmsUserController {
    @Resource
    IUmsUserService umsUserService;

    @Resource
    RedisTemplate<String,String> redisTemplate;


    @PostMapping("/token")
    CommonResult token(UmsUser user) throws JsonProcessingException {

        String token = umsUserService.token(user);
        if (StringUtils.isNotBlank(token)) {
            return CommonResult.success(token);
        } else {
            return CommonResult.failed("用户名或密码不正确");
        }
    }
    @GetMapping("getByMe")
    CommonResult getByMe(HttpServletRequest request){
        String token = request.getHeader("token");
        DecodedJWT jwt=JWT.decode(token);
        Integer id =jwt.getClaim("id").asInt();
        return CommonResult.success(umsUserService.getById(id));
    }
    @PostMapping("/logout")
    CommonResult logout(HttpServletRequest request) throws JsonProcessingException {
        return CommonResult.success( umsUserService.logout(request));
    }


    @GetMapping("list")
    CommonResult list(UmsUser user){
        return umsUserService.list(user);
    }
    @GetMapping("getById")
    CommonResult getById(UmsUser user){
        return CommonResult.success(umsUserService.getById(user.getId()));
    }
    @PostMapping("save")
    CommonResult save(UmsUser user){
        System.out.println("此时的user"+user);
        return umsUserService.saveOne(user);
    }
    @PostMapping("update")
    CommonResult update(UmsUser user){
        System.out.println("此时的user"+user);
        return umsUserService.update(user);
    }
    @PostMapping("back")
    CommonResult back(Integer id){
        return umsUserService.back(id);
    }
    @PostMapping("del")
    CommonResult del(Integer id){
        return umsUserService.del(id);
    }

}
